package com.tao.erp.procurement.action;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tao.erp.emp.pojo.Emp;
import com.tao.erp.procurement.pojo.Order;
import com.tao.erp.procurement.pojo.OrderDetail;
import com.tao.erp.procurement.service.OrderDetailService;
import com.tao.erp.procurement.service.OrderService;

@Controller
@RequestMapping("/order")
public class OrderAction {

	@Autowired
	private OrderService orderService;
	@Autowired
	private OrderDetailService orderDetailService;
	
	/**
	 * 
	 * @Description: TODO(异步加载Order集合)
	 * @author tao
	 * @date 2017-5-18 上午10:55:52
	 * @param order
	 * @return
	 */
	@RequestMapping("/selectOrderList.action")
	@ResponseBody
	public List<Order> selectOrderList(Order order){
		System.out.println(order);
		List<Order> list = orderService.selectOrderList(order);
		System.out.println(list);
		return list;
	}
	
	/**
	 * 
	 * @Description: TODO(查询OrderDetail返回页面)
	 * @author tao
	 * @date 2017-5-19 上午9:02:05
	 * @param uuid
	 * @param model
	 * @return
	 */
	@RequestMapping("/selectOrderDeteil.action")
	public String selectOrderDeteil(String uuid,Model model){
		Order order = orderService.selectOrderById(Integer.parseInt(uuid));
		System.out.println(order);
		model.addAttribute("order", order);
		return "/jsp/order/orderdetail.jsp";
	}
	
	/**
	 * 
	 * @Description: TODO(修改订单审核)
	 * @author tao
	 * @date 2017-5-19 下午7:45:59
	 * @param uuid
	 * @param model
	 * @return
	 */
	@RequestMapping("/selectOrderDeteil2.action")
	public String selectOrderDeteil2(String uuid,Model model){
		Order order = orderService.selectOrderById(Integer.parseInt(uuid));
		System.out.println(order);
		model.addAttribute("order", order);
		return "/jsp/order/orderdetailupdate.jsp";
	}
	
	/**
	 * 
	 * @Description: TODO(修改订单状态)
	 * @author tao
	 * @date 2017-5-19 下午7:58:46
	 * @param type
	 * @return
	 */
	@RequestMapping("/updateOrderDeteilByType.action")
	public String updateOrderDeteilByType(String type,String uuid){
		Order order = new Order();
		
		order.setUuid(Integer.parseInt(uuid));
		order.setType(Integer.parseInt(type));
		int i = orderService.updateOrder(order);
		System.out.println(i);
		return "/jsp/order/orderupdate.jsp";
	}
	
	/**
	 * 
	 * @Description: TODO(订单结单)
	 * @author tao
	 * @date 2017-5-23 下午9:39:37
	 * @param type
	 * @param uuid
	 * @return
	 */
	@RequestMapping("/updateOrderDeteilByType2.action")
	public String updateOrderDeteilByType2(String type,String uuid){
		Order order = new Order();
		
		order.setUuid(Integer.parseInt(uuid));
		order.setType(Integer.parseInt(type));
		int i = orderService.updateOrder(order);
		System.out.println(i);
		return "/jsp/task/selecttask.jsp";
	}
	
	/**
	 * 
	 * @Description: TODO(修改派单人)
	 * @author tao
	 * @date 2017-5-23 下午8:52:57
	 * @param completeruuid
	 * @param uuid
	 * @return
	 */
	@RequestMapping("/updateOrderDeteilByCompleter.action")
	public String updateOrderDeteilByCompleter(String completeruuid,String uuid){
		Order order = new Order();
		Emp emp = new Emp();
		emp.setUuid(Integer.parseInt(completeruuid));
		order.setUuid(Integer.parseInt(uuid));
		order.setCompleter(emp);
		int i = orderService.updateOrder(order);
		System.out.println(i);
		return "/jsp/task/task.jsp";
	}
	
	/**
	 * 
	 * @Description: TODO(查看订单返回taskdetail)
	 * @author tao
	 * @date 2017-5-23 下午8:11:58
	 * @param uuid
	 * @param model
	 * @return
	 */
	@RequestMapping("/selectOrderDeteil4.action")
	public String selectOrderDeteil4(String uuid,Model model){
		Order order = orderService.selectOrderById(Integer.parseInt(uuid));
		System.out.println(order);
		model.addAttribute("order", order);
		return "/jsp/task/taskdetail.jsp";
	}
	
	/**
	 * 
	 * @Description: TODO(查询订单返回selecttaskdetail)
	 * @author tao
	 * @date 2017-5-23 下午9:23:41
	 * @param uuid
	 * @param model
	 * @return
	 */
	@RequestMapping("/selectOrderDeteil3.action")
	public String selectOrderDeteil3(String uuid,Model model){
		Order order = orderService.selectOrderById(Integer.parseInt(uuid));
		System.out.println(order);
		model.addAttribute("order", order);
		return "/jsp/task/selecttaskdetail.jsp";
	}
	
	/**
	 * 
	 * @Description: TODO(添加订单)
	 * @author tao
	 * @date 2017-5-23 下午2:29:46
	 * @param supplieruuid
	 * @param znum
	 * @param selectGoods
	 * @param cpriceId
	 * @param num
	 * @param zprice
	 * @param request
	 * @return
	 */
	@RequestMapping("/insertOrder.action")
	public String insertOrder(String  supplieruuid,String znum, String[] selectGoods,String[] cpriceId,String[] num,String zprice,HttpServletRequest request){
		Order order = new Order();
		Emp emp = (Emp) request.getSession().getAttribute("sessionEmp");
		order.setOrdernum(UUID.randomUUID().toString().replace("-", "").substring(0,9));
		order.setCreater(emp);
		order.setCreatetime(new Date().getTime());
		order.setTotalnum(Integer.parseInt(znum));
		order.setTotalprice(Double.parseDouble(zprice));
		order.setSupplieruuid(Integer.parseInt(supplieruuid));
		order.setOrdertype(1);
		order.setType(111);
		int i = orderService.insertOrder(order);
		System.out.println(order.getUuid());
		for (int j = 0; j < selectGoods.length; j++) {
			OrderDetail detail = new OrderDetail();
			detail.setOrderuuid(order.getUuid());
			detail.setGoodsuuid(Integer.parseInt(selectGoods[j]));
			detail.setPrice(Double.parseDouble(cpriceId[j]));
			detail.setNum(Integer.parseInt(num[j]));
			orderDetailService.insertOrderDetail(detail);
		}
		return "/jsp/order/order.jsp";
	}
	
}
